using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using System;
using System.Threading.Tasks;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddGrpc();
builder.Services.AddGrpcHealthChecks()
    .AddAsyncCheck("", () =>
    {
        var result = Random.Shared.Next() % 5 == 0
            ? HealthCheckResult.Unhealthy()
            : HealthCheckResult.Healthy();

        return Task.FromResult(result);
    }, Array.Empty<string>());
builder.Services.Configure<HealthCheckPublisherOptions>(options =>
{
    options.Delay = TimeSpan.Zero;
    options.Period = TimeSpan.FromSeconds(5);
});

var app = builder.Build();
app.MapGrpcHealthChecksService();

app.Run();
